/**
 * index的方法
 * */

var phone = ''; //手机号
var code = ''; //码值
var token = ''; //登录token


//登录弹窗
function loginPop(){
    if (phone != '') {
        return;
    }
    var h = '';
    h += '<style type="text/css">#form { margin: 10px 10%; padding: 5%; width: 70%; background: #fff; border: 1px solid #ccc; border-radius: 10px; box-shadow: 0 0 10px #ccc; } #form .group { margin: 20px 0; } #form input { margin-top: 10px; padding: 10px 5%; width: 90%; border: 1px solid #0088cc; border-radius: 5px; font-size: 16px; } #form button { padding: 10px 0; width: 100%; background: #0088CC; color: #fff; border: 0; border-radius: 5px; }</style>';
    h += '<div id="form">';
    h += '<div class="group">';
    h += '输入您的手机号：';
    h += '<input type="text" placeholder="输入手机号" id="phone" value=""/>';
    h += '</div>';
    h += '<div class="group">';
    h += '<button type="button" onclick="dologin();">确定</button>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//设置手机号
function dologin(){
    var h = '';
    if (code == undefined || code == '') {
        h = '<div style="margin-top: 50%; width: ' + cw + 'px; height: ' + ch + 'px; text-align: center; color: #888;">';
        h += '无效码...';
        h += '</div>';
        $('body').html(h);
        return
    }
    var phone = $('#phone').val();
    if (phone == '') {
        msgPop({msg: '手机号必填'});
        return
    }
    var formData = {
        phone: phone,
        code: code
    };
    $.ajax({
        type: apis.userPhone.method,
        url: apis.userPhone.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            var userInfo = {
                token: res.data.token,
                uid: res.data.uid,
                code: res.data.code,
                phone: phone
            };
            setUserInfo({val: userInfo}); //设置本地缓存
            token = res.data.token;
            //console.log(res.data.view); return
            if (res.data.view == '') {
                window.location.href = routes.scence + '?code=' + code + '&token=' + token;
            } else {
                window.location.href = res.data.view + '?code=' + code+ '&token=' + token;
            }
        },
        error: function () {
            console.log('error');
        }
    });
}


//获取登录信息
function getInfoByPhone(){
    //var phone = userInfo.phone;
    var phone = '';
    if (userInfo && userInfo.phone) {
        phone = userInfo.phone;
    }
    var formData = {
        phone: phone,
        code: code
    };
    $.ajax({
        type: apis.infobyphone.method,
        url: apis.infobyphone.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            var userInfo = {
                token: res.data.token,
                uid: res.data.uid,
                code: res.data.code,
                phone: phone
            };
            setUserInfo({val: userInfo}); //设置本地缓存
            token = res.data.token;
            //console.log(res.data.view); return
            if (res.data.view == '') {
                window.location.href = routes.scence + '?code=' + code + '&token=' + token;
            } else {
                window.location.href = res.data.view + '?code=' + code+ '&token=' + token;
            }
        },
        error: function () {
            console.log('error');
        }
    });
}


//扫码记录
function qrlog(){
    var formData = {
        phone: phone,
        code: code
    };
    $.ajax({
        type: apis.qrlog.method,
        url: apis.qrlog.url,
        data: formData,
        success: function (res) {
            console.log(res);
        },
        error: function () {
            console.log('error');
        }
    });
}